package io.renren.modules.sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.renren.modules.sys.dao.SysRoleFlhDao;
import io.renren.modules.sys.entity.SysRoleFlhEntity;
import io.renren.modules.sys.service.SysRoleFlhService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import org.springframework.transaction.annotation.Transactional;


@Service("sysRoleFlhService")
public class SysRoleFlhServiceImpl extends ServiceImpl<SysRoleFlhDao, SysRoleFlhEntity> implements SysRoleFlhService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysRoleFlhEntity> page = this.page(
                new Query<SysRoleFlhEntity>().getPage(params),
                new QueryWrapper<SysRoleFlhEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Long roleId, List<String> flhs) {
        baseMapper.delete(new UpdateWrapper<SysRoleFlhEntity>().eq("role_id",roleId.intValue()));
        if(CollectionUtil.isNotEmpty(flhs)){
            flhs.forEach(flh->{
                SysRoleFlhEntity sysRoleFlhEntity=new SysRoleFlhEntity();
                sysRoleFlhEntity.setFlh(flh);
                sysRoleFlhEntity.setRoleId(roleId.intValue());
                baseMapper.insert(sysRoleFlhEntity);
            });
        }
    }

}
